home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3063 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: Rezonet.net!news
  2. From: ray@ultimate-tech.com (Ray Dunn)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Basic Question on SWITCH
  5. Date: 25 Jan 1996 20:29:09 GMT
  6. Organization: Ultimate Technographics Inc.
  7. Message-ID: <4e8p6m$n8q@ns.RezoNet.NET>
  8. References: <4e4cu4$95f@vixen.cso.uiuc.edu>
  9. NNTP-Posting-Host: 204.19.230.7
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-Newsreader: WinVN 0.99.7
  13.  
  14. In referenced article, HOTARD says...
  15. >I am just learning how to program in C, and I had a question about 
  16. >switch.
  17. >I am writing a program that looks at a character and then determines 
  18. >if it is a letter or number.  This program must use the switch
  19. >command , can I place a range on the case command somehow??
  20. >
  21. >ie:  Switch (var)
  22. >        case 0-9:
  23. >
  24. >or case (isdigit):  
  25. >
  26. >would anything like this work???
  27.  
  28. No, the only thing you can do is to give a series of different case 
  29. labels, like:
  30.  
  31.   switch (character)
  32.   {
  33.    case '0':
  34.    case '1':
  35.    case '2':
  36.    [etc]
  37.    case '9':
  38.     [process a digit here]
  39.     break;
  40.  
  41.    case 'a':
  42.    case 'b':
  43.    etc.
  44.  
  45.    default:
  46.      [none of the above]
  47.  
  48. A switch is not a good choice for what you're trying to do - although 
  49. it might produce quite efficient code, the source is very verbose.
  50.  
  51. If you must use switches for your exercise, you *could* say:
  52.  
  53.   switch (isdigit(character))
  54.   {
  55.     case 0:
  56.       switch (isalpha(character))
  57.       {
  58.         case 0:
  59.       [process not a digit nor an alpha]
  60.           break;
  61.  
  62.         default:
  63.           [process the alpha];
  64.       }
  65.       break;
  66.  
  67.     default:
  68.       [process the digit]
  69.   }
  70.  
  71. but this is a pretty obscure piece of code!!  Note that you can't use 
  72. "case 1:" to get the isalpha or isdigit case, because these return zero 
  73. or non-zero,  not zero or one.
  74.  
  75. [Note: your From: header line does not contain a valid email address]
  76. -- 
  77. Ray Dunn (opinions are my own) | Phone: (514) 938 9050
  78. Montreal                       | Phax : (514) 938 5225
  79. ray@ultimate-tech.com          | Home : (514) 630 3749
  80.  
  81.